﻿using System;
using System.Collections.Generic;
using System.Text;
using GoldenAgeHelper.common;
using System.Text.RegularExpressions;
using GoldenAgeHelper.DAL;
using System.Threading;

namespace GoldenAgeHelper
{
    public class GAPosition
    {
        public static int cid = 0;
        public static string Building(int id, string action)
        {
            string a = "";
            if (!string.IsNullOrEmpty(action))
            {
                a = "action=" + action + "&";
            }
            string url = GA.ROOT_URL + "/building" + id + "?" + a + "ts=" + Util.GetTime();
            string text = GAClient.DownloadString(url);
            Thread.Sleep(500);
            return text;
        }

        public static string Building(int id)
        {
            return Building(id, null);
        }

        public static string Envir(int id)
        {
            //http://t1.nexttime.cn/envir28?ts=1274929913718
            string text = GAClient.DownloadString(GA.ROOT_URL + "/envir" + id + "?ts=" + Util.GetTime());
            //收集场景信息
            if (true)
            {
                //>楚州郊外</a>
                string cityname = GARegex.GetString(text, "<h3><a href='#' u='/environ'>", "郊外</a>");
                int cid = City.GetCityID(cityname);
                //需要技能：[ 旅游 ] <span>101</span>
                MatchCollection coll = GARegex.Matches(text, "需要技能：\\[ (?<name>[\\s\\S]+?) \\] <span>(?<value>[\\d+])</span>");
                MatchCollection coll2 = GARegex.Matches(text, "/sys/i/h/(?<id>\\d+?).gif' class='pic' />(?<name>[\\s\\S]+?)</a>");
                for (int i = 0; i < coll.Count; i++)
                {
                    EnvirConfig.AddEnvirConfig(cid, id, coll[i].Groups["name"].Value, Convert.ToInt32(coll[i].Groups["value"].Value), coll.Count == coll2.Count ? coll2[i].Groups["name"].Value : "");
                }
                //需要技能：[ 采集 ] 1</p>
                coll = GARegex.Matches(text, "需要技能：\\[ (?<name>[\\s\\S]+?) \\] (?<value>[\\d+])</p>");
                for (int i = 0; i < coll.Count; i++)
                {
                    EnvirConfig.AddEnvirConfig(cid, id, coll[i].Groups["name"].Value, Convert.ToInt32(coll[i].Groups["value"].Value), coll.Count == coll2.Count ? coll2[i].Groups["name"].Value : "");
                }
                GA.AddMessage("共收集到[" + coll.Count + "]个场景信息。");
            }
            return text;
        }

        public static string Envir(Envir envir)
        {
            return Envir((int)envir);
        }

        public static string environ()
        {
            string text = GAClient.DownloadString(GA.ROOT_URL + "/environ?ts=" + Util.GetTime());
            return text;
        }

    }
}
